﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using NPOI.SS.Util;

namespace IHISCETeim.Web.Export.FTradeModel
{
    public partial class FTradeExportInvoice : ExportPageBase
    {
        protected override string xlsTemplateFileName
        {
            get
            {
                return "应收帐款_财务部发票签收.xls";
            }
        }
        protected override string SheetName
        {
            get
            {
                return "模板";
            }
        }


        protected string CreateDateFrom
        {
            get
            {

                return GetRequestValue<string>("CreateDateFrom", DateTime.Now.ToString("yyyy-MM-01"));
            }
        }
        protected string CreateDateTo
        {
            get
            {


                return GetRequestValue<string>("CreateDateTo", DateTime.Now.ToString("yyyy-MM-dd")) + " 23:59:59";

            }
        }

        protected DataTable sumdt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
         
            DataTableCollection dts =  FTradeReportController.FTradeExportGetInvoice(CreateDateFrom,CreateDateTo);

            DataTable allAgentlist = dts[0];
            DataTable exportDT = dts[1];
            DataTable sboagentlist = SBOController.GetAgentList();//所有经销商列表

            if (allAgentlist.Rows.Count <= 0)
            {
                Response.Write("没有任何数据...");
                return;
            }

            InitializeWorkbook();

            string AliasName = "";
            int sheetindex = 1;//从第二个工作表开始
            foreach (DataRow dr in allAgentlist.Rows)
            {
                AliasName = dr["JingXiaoShang"].ToString();
                DataTable detaildt = new DataTable();
                if (AliasName == "湖南金力源")
                {
                    detaildt = exportDT.Select("JingXiaoShang in('" + AliasName + "','湖南金土地')").CopyToDataTable();
                }
                else
                {
                    detaildt = exportDT.Select("JingXiaoShang='" + AliasName + "'").CopyToDataTable();
                }

                ISheet sheet2 = hssfworkbook.CloneSheet(0);
                hssfworkbook.SetSheetName(sheetindex, AliasName+Convert.ToDateTime(CreateDateTo).ToString("(MM月)"));
                //经销商全称                
                DataRow[] drs = sboagentlist.Select(string.Format(" AliasName ='{0}'", AliasName.Trim()));
                string cardname = drs[0]["CardName"].ToString();

                if (detaildt.Rows.Count <= 0)
                    continue;
                WriteSheet(sheet2, detaildt, cardname);

                sheet2.ForceFormulaRecalculation = true;
                sheetindex++;
            }
            //隐藏
            hssfworkbook.RemoveSheetAt(0);
            //
            WriteToFile();
        }


        private void WriteSheet(ISheet sheet2, DataTable exportDT, string cardname)
        {
            //兹收到石川岛中骏（厦门）建机有限公司开具的增值税专用发票及抵扣联各3份。
            sheet2.GetRow(1).GetCell(1).SetCellValue(string.Format("兹收到石川岛中骏（厦门）建机有限公司开具的增值税专用发票及抵扣联各{0}份。", exportDT.Rows.Count));
            //签收单位：
            sheet2.GetRow(11).GetCell(2).SetCellValue(cardname);            

            //12号字体样式
            IFont fontStyleRich2 = hssfworkbook.CreateFont();
            fontStyleRich2.FontHeightInPoints = 12;
            //开始FOR各合同信息
            if (exportDT.Rows.Count >3)
            {
                HSSFRow 源格式行 = (HSSFRow)sheet2.GetRow(4);
                MyInsertRow((HSSFSheet)sheet2, 6, exportDT.Rows.Count - 3, 源格式行);
            }
            int rowindexBase = 4;
            int rowindex = 0;
            int rowReal = rowindex + rowindexBase;
            ICellStyle style12 = hssfworkbook.CreateCellStyle();
            style12.SetFont(fontStyleRich2);
            //水平对齐  
            style12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
            //垂直对齐  
            style12.VerticalAlignment = VerticalAlignment.CENTER;
            //自动换行  
            style12.WrapText = true;
            
            foreach (DataRow dr in exportDT.Rows)
            {
                HSSFRow row1 = (HSSFRow)sheet2.GetRow(rowReal);
                //合并处理
                //  sheet2.AddMergedRegion(new CellRangeAddress(rowReal, rowReal, 4, 5));
                // row1.GetCell(4).CellStyle = style12;

                row1.GetCell(0).SetCellValue(rowindex+1);
                row1.GetCell(1).SetCellValue(Convert.ToDateTime(dr["FPDate"]).ToString("yyyy-MM-dd"));
                row1.GetCell(2).SetCellValue(dr["FPDaiMa"].ToString());
                row1.GetCell(3).SetCellValue(dr["FPHaoMa"].ToString());
                row1.GetCell(4).SetCellValue(dr["XingHao"].ToString());
                row1.GetCell(5).SetCellValue(dr["ShuLiang"].ToString());
                row1.GetCell(6).SetCellValue(dr["FPJinE"].ToString());
                row1.GetCell(7).SetCellValue(dr["JiHao"].ToString());
                row1.GetCell(8).SetCellValue(dr["FPRemark"].ToString());

                rowindex++;
                rowReal = rowindex + rowindexBase;
            }

        }


      
        protected override string GetXlsFileName(string oldFileName)
        {
            //return base.GetXlsFileName(oldFileName);

            return string.Format("发票签收单{0}.xls",Convert.ToDateTime(CreateDateTo).ToString("MM月"));
        }

    }
}